﻿//作者：       minjie.zhou
// 创建时间：   2012/12/11 23:29:05
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Entity;
using Data;
using Entity;

namespace AgileToDo
{
    /// <summary>
    /// PageMain.xaml 的交互逻辑
    /// </summary>
    public partial class PageMain : Page
    {
        public PageMain()
        {
            InitializeComponent();
        }



        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var project in DbManager.ToDoContext.Projects.Include(p => p.ToDos))
            {
                var item = new UPItem();
                item.DataContext = project;
                item.Click += new EventHandler(item_Click);
                this.projectItems.Children.Add(item);
            }

        }

        private Project _selectPj;
        protected void item_Click(object sender, EventArgs e)
        {
            var selectedItem = sender as UPItem;
            var project = selectedItem.DataContext as Project;
            if (_selectPj != null && _selectPj.ProjectID == project.ProjectID)
                return;

            _selectPj = project;
            foreach (var child in this.projectItems.Children)
            {
                var item = child as UPItem;
                if (item.Equals(selectedItem))
                    continue;
                item.Selected = false;
            }

            this.toList.Children.Clear();
            if (_selectPj.ToDos != null)
                foreach (var todo in _selectPj.ToDos.OrderBy(o => o.FinishTime.HasValue).ThenByDescending(o => o.CreateTime))
                {
                    var item = new UTodoItem();
                    item.DataContext = todo;
                    this.toList.Children.Add(item);
                    Debug.WriteLine(todo.ToDoName);
                }
        }


        private void Image_MouseLeftButtonDown_AddToDo(object sender, MouseButtonEventArgs e)
        {
            if (_selectPj == null)
                return;

            var toDo = new ToDo() { ProjectID = _selectPj.ProjectID, ToDoName = "", CreateTime = DateTime.Now };
            DbManager.ToDoContext.ToDos.Add(toDo);

            var item = new UTodoItem();
            item.DataContext = toDo;
            this.toList.Children.Insert(0, item);
        }
        private void Image_MouseLeftButtonDown_AddProj(object sender, MouseButtonEventArgs e)
        {
            var pj = new Project() { ProjectName = "", CreateTime = DateTime.Now };
            DbManager.ToDoContext.Projects.Add(pj);

            var item = new UPItem();
            item.DataContext = pj;
            item.Click += new EventHandler(item_Click);
            this.projectItems.Children.Insert(0, item);
        }
    }
}
